package me.stone.stanimeclient

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.widget.Toolbar
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import com.didi.drouter.api.DRouter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import me.stone.stanimeclient.base.AbsViewBindingActivity
import me.stone.stanimeclient.base.INavigationHost
import me.stone.stanimeclient.core.common.extensions.dip
import me.stone.stanimeclient.core.common.extensions.launchAndRepeatWithLifecycle
import me.stone.stanimeclient.databinding.ActivityMainBinding
import kotlin.math.roundToInt

@AndroidEntryPoint
class MainActivity: AbsViewBindingActivity<ActivityMainBinding>(), INavigationHost {

    private val viewModel: MainViewModel by viewModels()

    override fun buildViewBinding() = ActivityMainBinding.inflate(layoutInflater).apply {
        viewModel = this@MainActivity.viewModel
    }

    override fun hasBottomNavigationView() = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        navController?.let { binding.navigation.setupWithNavController(it) }
        navController?.addOnDestinationChangedListener { _, destination, _ ->
            when (destination.id) {
                R.id.nav_setting, R.id.nav_libraries -> {
                    showHideToolbarMenu(false)
                }
                else -> showHideToolbarMenu(true)
            }
        }

        binding.toolbar.titleMarginTop = 16f.dip.roundToInt()

        binding.searchButton.setOnClickListener {
            DRouter.build(Constants.DROUTE_PATH_FAN_SEARCH)
                .putExtra(Constants.DROUTE_EXTRA_KEY_CONTENT,
                    viewModel.recommendQuery.value)
                .start(this@MainActivity)
        }

        launchAndRepeatWithLifecycle {
            launch {
                viewModel.seeFanDetail.collect {
                    DRouter.build(Constants.DROUTE_PATH_FAN_INFO)
                        .putExtra(Constants.DROUTE_EXTRA_KEY_ID, it)
                        .start(this@MainActivity)
                }
            }

            launch {
                viewModel.recommendQuery.collectLatest {
                    binding.searchButton.text = it
                }
            }
        }

        registerToolbarWithNavigation(binding.toolbar)
    }

    private val topLevelDestinations = setOf(
        R.id.nav_home,
        R.id.nav_catalog,
        R.id.nav_setting
    )

    private fun registerToolbarWithNavigation(toolbar: Toolbar) {
        navController?.let {
            val appBarConfiguration = AppBarConfiguration(topLevelDestinations)
            toolbar.setupWithNavController(it, appBarConfiguration)
        }
    }

    private fun showHideToolbarMenu(show: Boolean) {
        binding.searchButton.visibility = if (show) View.VISIBLE else View.GONE
        binding.toolbar.menu.findItem(R.id.history).isVisible = show
    }

    override fun getToolbar(): Toolbar {
        return binding.toolbar
    }

}